package net.minidev.json.mapper;

/*
 *    Copyright 2011 JSON-SMART authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.lang.reflect.Type;
import java.util.HashMap;

import net.minidev.asm.Accessor;
import net.minidev.asm.BeansAccess;
import net.minidev.json.JSONUtil;

public abstract class BeansMapper<T> extends AMapper<T> {

	public abstract Object getValue(Object current, String key);
	
	public static class Bean<T> extends AMapper<T> {
		Class<T> clz;
		BeansAccess ba;

		HashMap<String, Accessor> index = new HashMap<String, Accessor>();

		public Bean(Class<T> clz) {
			this.clz = clz;
			ba = BeansAccess.get(clz);
			index = ba.getMap();
		}

		@Override
		public void setValue(Object current, String key, Object value) {
			Accessor nfo = index.get(key);
			if (nfo == null)
				throw new RuntimeException("Can not set " + key + " field in " + clz);
			value = JSONUtil.convertTo(value, nfo.getType());
			ba.set(current, nfo.getIndex(), value);
		}

		public Object getValue(Object current, String key) {
			Accessor nfo = index.get(key);
			if (nfo == null)
				throw new RuntimeException("Can not set " + key + " field in " + clz);
			return ba.get(current, nfo.getIndex());
		}
		@Override
		public Type getType(String key) {
			Accessor nfo = index.get(key);
			return nfo.getGenericType();
		}

		@Override
		public AMapper<?> startArray(String key) {
			Accessor nfo = index.get(key);
			if (nfo == null)
				throw new RuntimeException("Can not set " + key + " field in " + clz);
			return Mapper.getMapper(nfo.getGenericType());
		}

		@Override
		public AMapper<?> startObject(String key) {
			Accessor f = index.get(key);
			if (f == null)
				throw new RuntimeException("Can not set " + key + " field in " + clz);
			return Mapper.getMapper(f.getGenericType());
		}

		@Override
		public Object createObject() {
			return ba.newInstance();
		}
	}
}
